< previous page page_185 next page >

Page 185
Determining the Length of the String
For many operations, you may need to know how many characters are in a string. You might need this information to know whether the string with which you are working will fit in a fixed-length database field. Or, if you are working with big strings, you may want to make sure that the combined size of the two strings does not exceed the capacity of the string variable. In any case, to determine the length of any string, you use the Len function, as illustrated in the following code line:
result = Len(inputstr)
You can use the Len function in a number of applications. In many cases, it is used to determine whether any characters appear in a string. If no characters exist, you might want to issue an error message or at least bypass any further processing.
Changing the Case of a String
Two functions can modify the case of letters in a string: UCase and LCase. The UCase function returns a string with all the letters converted to uppercase (capital) letters. The LCase function does just the opposite, converting the entire string to lowercase letters.
Although these functions may appear to be somewhat trivial, they actually are quite useful for a number of tasks. First, you can use the functions to properly capitalize names or other words that a user may enter. The code in Listing 9.3 capitalizes the first letter of a word and makes the rest of the word lowercase.
Listing 9.3 CASECONV.ZIPUsing UCase and LCase to Capitalize a Word Properly
Dim sWord as String, sProperWord as String
sWord="mIxEd CaSe"
sProperWord = UCase$(Left$(sWord, 1))
sProperWord = sProperWord & LCase$(Mid$(sWord,2))
note.gif The string functions return values, but they do not modify the input. Consider the following example with the UCase function:
Dim s1 As String
Dim s2 As String
s1 = "which case am i"
s2 = UCase$(s1)
After this code is executed, the variable s2 appears in all uppercase letters, whereas s1 remains unchanged, unless you put s1 on both sides of the assignment statement, like this:
s1 = UCase$(s1) square.gif

The case functions are useful, for example, when you are comparing user input to a predefined value or a range of values. If you convert the user's input to uppercase, you can compare it to an uppercase test string, as in the following example:

 
< previous page page_185 next page >